﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
public partial class backstage_admin : System.Web.UI.Page
{
    // Fields
    protected Admin ad = new Admin();
    protected AdminGroupManager agm = new AdminGroupManager();
    protected AdminManager am = new AdminManager();

    // Methods
    protected StringBuilder AdminGroupBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable allAdminGroup = this.agm.GetAllAdminGroup();
            if (allAdminGroup.Rows.Count <= 0)
            {
                return builder;
            }
            foreach (DataRow row in allAdminGroup.Rows)
            {
                builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["groupName"].ToString() + "</option>");
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.ad.UserName = this.txtUserName.Value;
            if (this.txtUserPwd.Value != "")
            {
                this.ad.UserPwd = Gift.Security.DESEncrypt.Encrypt(this.txtUserPwd.Value);
            }
            this.ad.Email = this.txtEmail.Value;
            this.ad.Name = this.txtName.Value;
            this.ad.Depart = this.txtDepart.Value;
            this.ad.GroupId = Convert.ToInt32(base.Request.Form["adminGroup"]);
            this.ad.State = Convert.ToInt32(base.Request.Form["state"]);
            this.ad.RegisterIP = new ClinetMAC().GetIPAddress();
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.ad.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.am.ModifyAdminInfo(this.ad))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"admin_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                }
            }
            else if (this.am.AdminRegister(this.ad))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"admin_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
            }
        }
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["ot"] == null)
            {
                return builder;
            }
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                builder.Append("编辑管理员");
                return builder;
            }
            builder.Append("添加管理员");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.UserRankBind();
            }
            else
            {
                this.rdState1.Checked = true;
            }
        }
    }

    private void UserRankBind()
    {
        try
        {
            DataTable adminById = this.am.GetAdminById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (adminById.Rows.Count > 0)
            {
                this.txtUserName.Value = adminById.Rows[0]["userName"].ToString();
                this.txtUserName.Attributes.Add("readonly", "readonly");
                this.txtEmail.Value = adminById.Rows[0]["email"].ToString();
                this.txtName.Value = adminById.Rows[0]["name"].ToString();
                this.txtDepart.Value = adminById.Rows[0]["depart"].ToString();
                base.ClientScript.RegisterStartupScript(base.GetType(), "sag", "<script type=\"text/javascript\">selectIsExitItem(\"selectAdminGroup\",\"" + adminById.Rows[0]["adminGroup"].ToString() + "\");</script>");
                if (Convert.ToInt32(adminById.Rows[0]["state"]) == 1)
                {
                    this.rdState1.Checked = true;
                }
                else
                {
                    this.rdState0.Checked = true;
                }
            }
        }
        catch
        {
        }
    }

}